package com.jumi.microservice.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumi.microservice.common.constant.RequestConstant;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.constant.StatusEnum;
import com.jumi.microservice.domain.Article;
import com.jumi.microservice.domain.ArticleClass;
import com.jumi.microservice.domain.dto.ArticleDTO;
import com.jumi.microservice.domain.vo.ArticleOP;
import com.jumi.microservice.domain.vo.ArticleQuery;
import com.jumi.microservice.mapper.ArticleClassMapper;
import com.jumi.microservice.mapper.ArticleMapper;
import com.jumi.microservice.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private ArticleClassMapper articleClassMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void insert(ArticleOP op, HttpServletRequest request) {
        ExceptionEnum.FAIL_NULL.doThrowIf(op==null);
        //判断文章分类是否存在
        int classCount = articleClassMapper.selectCount(
                 Wrappers.<ArticleClass>lambdaQuery()
                .eq(ArticleClass::getAcid,op.getAcid())
                .eq(ArticleClass::getIsDel,0)
         );
        ExceptionEnum.FAIL_ARTICLE_CLASS.doThrowIf(classCount <= 0);
        //判断文章标题是否重名
        int titleConunt = baseMapper.selectCount(
                Wrappers.<Article>lambdaQuery()
                .eq(Article::getArticleTitle,op.getArticleTitle())
                .eq(Article::getIsDel,StatusEnum.STATUS_NO.getCode())
        );
        ExceptionEnum.FAIL_ARTICLE_TITLE.doThrowIf(titleConunt > 0);

        Article article = new Article();
        article.setAcid(op.getAcid());
        article.setArticleTitle(op.getArticleTitle());
        article.setArticleContent(op.getArticleContent());
        article.setSort(op.getSort());
        article.setCreater(request.getHeader(RequestConstant.ADMIN_NAME));
        article.setModifier(request.getHeader(RequestConstant.ADMIN_NAME));
        baseMapper.insert(article);
    }

    @Override
    public void update(ArticleOP op, HttpServletRequest request) {
        ExceptionEnum.FAIL_NULL.doThrowIf(op==null);
        //判断传过来id是否能查到信息
        Article art = baseMapper.selectById(op.getId());
        ExceptionEnum.FAIL_ID_NULL.doThrowIf(art == null);
        //判断文章分类是否存在
        int classCount = articleClassMapper.selectCount(
                Wrappers.<ArticleClass>lambdaQuery()
                        .eq(ArticleClass::getAcid,op.getAcid())
                        .eq(ArticleClass::getIsDel,StatusEnum.STATUS_NO.getCode())
        );
        ExceptionEnum.FAIL_ARTICLE_CLASS.doThrowIf(classCount <= 0);
        //判断文章标题是否重名
        int titleConunt = baseMapper.selectCount(
                Wrappers.<Article>lambdaQuery()
                        .eq(Article::getArticleTitle,op.getArticleTitle())
                        .eq(Article::getIsDel,StatusEnum.STATUS_NO.getCode())
                        .ne(Article::getId,op.getId())
        );
        ExceptionEnum.FAIL_ARTICLE_TITLE.doThrowIf(titleConunt > 0);
        Article article = new Article();
        article.setId(op.getId());
        article.setAcid(op.getAcid());
        article.setArticleTitle(op.getArticleTitle());
        article.setArticleContent(op.getArticleContent());
        article.setSort(op.getSort());
        article.setModifier(request.getHeader(RequestConstant.ADMIN_NAME));
        baseMapper.updateById(article);
    }

    @Override
    public void delete(Long id,HttpServletRequest request) {
        Article article = baseMapper.selectById(id);
        //判断传过来id是否能查到信息
        ExceptionEnum.FAIL_ID_NULL.doThrowIf(article == null);
        //启用状态下的文章，不能删除
        ExceptionEnum.FAIL_ARTICLE_STATUS.doThrowIf(article.getStatus() == StatusEnum.STATUS_YES.getCode());

        article.setIsDel(StatusEnum.STATUS_YES.getCode());
        article.setModifier(request.getHeader(RequestConstant.ADMIN_NAME));
        baseMapper.updateById(article);
    }

    @Override
    public void status(Long id, Integer status, HttpServletRequest request) {
        Article article = baseMapper.selectById(id);
        //判断传过来id是否能查到信息
        ExceptionEnum.FAIL_ID_NULL.doThrowIf(article == null);

        article.setStatus(status);
        article.setModifier(request.getHeader(RequestConstant.ADMIN_NAME));
        baseMapper.updateById(article);
    }


    @Override
    public ArticleDTO selectId(Long id) {
        //判断传过来的id是否为空
        ExceptionEnum.FAIL_ID_NULL.doThrowIf(id==null);
        //查询文章数据
        Article article = baseMapper.selectById(id);
        //判断传过来id是否能查到信息
        ExceptionEnum.FAIL_ID_NULL.doThrowIf(article == null);
        return po2Dto(article);
    }

    /**
     * 对象转换 po->dto
     * @param po
     * @return
     */
    private ArticleDTO po2Dto(Article po){
        ArticleDTO r = null;
        try {
            r = po.clone(ArticleDTO.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return r;
    }

    /**
     * 对象转换 dto->do
     * @param dto
     * @return
     */
    private Article dto2Po(ArticleDTO dto){
        Article r = null;
        try {
            r = dto.clone(Article.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return r;
    }
}
